package com.example.minzhengtong.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.minzhengtong.Const
import com.example.minzhengtong.R
import com.example.minzhengtong.data.*
import com.example.minzhengtong.databinding.CommonListBinding
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.SpUtil
import com.example.minzhengtong.utils.WUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 *@author: wangtianxiong
 *@time: 2022/5/11
 *@describe:
 *
 */
class SocialFGNew : Fragment() {
    companion object {
        @JvmStatic
        fun newInstance(tag: Int = 0) =
            SocialFGNew().apply {
                arguments = Bundle().apply {
                    putInt("tag", tag)
                }
            }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = CommonListBinding.inflate(inflater)
        return binding.root
    }
    private lateinit var binding : CommonListBinding

    private val userBean by lazy {
        SpUtil.getInstance().getObject(Const.USER_BEAN) as UserBean
    }
    private val mAdapter1 by lazy {
        SocialAdapter1()
    }
    private val mAdapter2 by lazy {
        SocialAdapter2()
    }
    private val mAdapter3 by lazy {
        SocialAdapter3()
    }

    private val pageTag by lazy {
        arguments!!.getInt("tag")!!
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        qyzgBean.addAll( Gson().fromJson(userBean.data.qyzgylx,
            object : TypeToken<List<QyzgylxBean>>() {}.type))
        LogUtil.d("企业职工保险${userBean.data.qyzgylx}")
        LogUtil.d("企业职工保险${qyzgBean}")

        syBean.addAll(
            Gson().fromJson(userBean.data.sybx,
            object : TypeToken<List<SybxBean>>() {}.type))
        LogUtil.d("失业保险${userBean.data.sybx}")
        LogUtil.d("失业保险${syBean}")

        gsBean.addAll(
            Gson().fromJson(userBean.data.gsbx,
            object : TypeToken<List<GsbxBean>>() {}.type))
        LogUtil.d("工伤保险${userBean.data.gsbx}")
        LogUtil.d("工伤保险${gsBean}")
        binding.recyclerView.layoutManager = WUtils.verManager(activity)
        when(pageTag){
            0 -> {
                binding.recyclerView.adapter = mAdapter1
            }
            1 -> {
                binding.recyclerView.adapter = mAdapter2
            }
            2 -> {
                binding.recyclerView.adapter = mAdapter3
            }
        }
    }
    private val qyzgBean = mutableListOf<QyzgylxBean>()
    private val syBean = mutableListOf<SybxBean>()
    private val gsBean = mutableListOf<GsbxBean>()
    inner class SocialAdapter1 : CommonAdapter<QyzgylxBean>(
        activity,
        R.layout.item_social1,
        qyzgBean
    ) {
        override fun convert(holder: ViewHolder?, t: QyzgylxBean?, position: Int) {
            Log.d("TAG", "convert: "+qyzgBean[position].riqi)
            holder?.setText(R.id.time, qyzgBean[position].riqi)
            holder?.setText(R.id.company, qyzgBean[position].gsmc)
            holder?.setText(R.id.group, qyzgBean[position].gsjn)
            holder?.setText(R.id.person, qyzgBean[position].grjn)
            holder?.setText(R.id.income, qyzgBean[position].zhhr)
        }
    }

    inner class SocialAdapter2 : CommonAdapter<GsbxBean>(
        activity,
        R.layout.item_social2,
        gsBean
    ) {
        override fun convert(holder: ViewHolder?, t: GsbxBean?, position: Int) {
            holder?.setText(R.id.time, gsBean[position].riqi)
            holder?.setText(R.id.company, gsBean[position].gsmc)
            holder?.setText(R.id.group, gsBean[position].zhhr)
        }
    }

    inner class SocialAdapter3 : CommonAdapter<SybxBean>(
        activity,
        R.layout.item_social3,
        syBean
    ) {
        override fun convert(holder: ViewHolder?, t: SybxBean?, position: Int) {
            holder?.setText(R.id.company, syBean[position].gsmc)
            holder?.setText(R.id.time, syBean[position].riqi)
            holder?.setText(R.id.group, syBean[position].gsjn)
            holder?.setText(R.id.person, syBean[position].grjn)
        }
    }
}